package com.oneline.web.components {
	
	import com.google.maps.Color;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	
	public class WFShape extends UIComponent
	{
		public static var PROCESS:uint = 0;
		public static var START:uint = 1;
		public static var END:uint = 2;
		public static var DECISION:uint = 3;
		public static var DOCUMENT:uint = 4;
		public static var DATA:uint = 5;
		public static var PREDEFINED_PROCESS:uint = 6;
		public static var STORED_DATA:uint = 7;
		public static var DIRECT_DATA:uint = 8;
		public static var MANUAL_INPUT:uint = 9;
		public static var CARD:uint = 10;
		public static var PAPER_TAPE:uint = 11;
		public static var DELAY:uint = 12;
		public static var MANUAL_OPERATION:uint = 13;
		public static var PREPARATION:uint = 14;
		public static var TERMINATION:uint = 15;
		public static var OFFPAGE:uint = 16;
		public static var BLANK:uint = 17;
		public static var ECLIPSE:uint = 18;

		private var vector:Shape = new Shape();
		private var g:Graphics = null;
		private var txt:TextField = new TextField();
		
		private var _type:uint = 0;
		private var _color:uint = 0xFFFFFF;
		private var _bcolor:uint = 0x000000;
		private var _label:String = "Start" ;
		private var _angle:int = 0;
		
		public function WFShape() {
			super();
			this.addChild(vector);
			this.g = vector.graphics;
			this.txt.wordWrap =  true;
			this.txt.autoSize = TextFieldAutoSize.CENTER;
			this.addChild(txt); 
			this.addEventListener(Event.RESIZE, redraw);
		}
		
		public function get type():uint {
			return this._type;
		}

		public function set type(t:uint):void {
			this._type = t;
			this.refresh();
		}
		
		public function get color():uint {
			return this._color;
		}

		public function set color(c:uint):void {
			this._color = c;
			this.refresh();
		}		
		
		public function get angle():uint {
			return this._color;
		}

		public function set angle(a:uint):void {
			this._angle = a;
			this.refresh();
		}		

		public function get bcolor():uint {
			return this._bcolor;
		}

		public function set bcolor(c:uint):void {
			this._bcolor = c;
			this.refresh();
		}		

		public function get label():String {
			return this._label;
		}

		public function set label(l:String):void {
			this._label = l;
			refreshText();
		}
		
		private function redraw(event:Event):void {
			this.refresh();
		}
		
		public function refresh(): void {
			
			this.g.clear();
			g.beginFill( _color, 1 );
    		g.lineStyle(1, _bcolor, 1);
			
			switch(_type) {
				case PROCESS:
					drawProcess();
					break;
				case START:
					this.drawStart();
					break;
				case END:
					this.drawEnd();
					break;
				case DECISION:
					drawDecision();
					break;
				case DOCUMENT:
					drawDocument();
					break;
				case DATA:
					drawData();
					break;
				case PREDEFINED_PROCESS:
					drawPredefinedProcess();
					break;
				case STORED_DATA:
					drawStoredData();
					break;
				case DIRECT_DATA:
					drawDirectData();
					break;
				case MANUAL_INPUT:
					drawManualInput();
					break;
				case CARD:
					drawCard();
					break;
				case PAPER_TAPE:
					drawPaperTape();
					break;
				case DELAY:
					delay();
					break;
				case MANUAL_OPERATION:
					manualOperation();
					break;
				case PREPARATION:
					preparation();
					break;
				case TERMINATION:
					terminator();
					break;
				case OFFPAGE:
					offPage();
					break;
				case ECLIPSE:
					ellipse();
					break;
			}
			g.endFill();
			this.vector.rotation=_angle;
			this.refreshText();
		}
		
		private function refreshText():void {
			if ( null == this._label ) return;
			this.txt.htmlText = this._label; 
			this.txt.x = ( this.width - txt.textWidth ) / 2 - 6;
			this.txt.y = ( this.height - txt.textHeight ) / 2 - 6;			
		}
		

		private function drawLine3(x1:int, y1:int, x2:int, y2:int, x3:int, y3:int): void {
      		g.lineTo(x1, y1);
      		g.lineTo(x2, y2);
      		g.lineTo(x3, y3);
		}
		
		private function drawLine4(x1:int, y1:int, x2:int, y2:int, x3:int, y3:int, x4:int, y4:int): void {
      		g.lineTo(x1, y1);
      		g.lineTo(x2, y2);
      		g.lineTo(x3, y3);
      		g.lineTo(x4, y4);
		}

		public function drawStart(): void {
			var r:int = ( this.height > this.width ) ? this.width/2 : this.height / 2;
			g.drawCircle( this.width/2, this.height/2,  r);
		}
		
		public function drawEnd(): void {
			var r:int = ( this.height > this.width ) ? this.width/2 : this.height / 2;
			g.endFill();
			g.drawCircle( this.width/2, this.height/2,  r);
			var ir:int = r/2;
			ir = ( ir == 0 ) ? r : ir; 
			g.beginFill( _color, 1 );
			g.drawCircle( this.width/2, this.height/2,  ir);
			g.endFill();
		}

		public function ellipse(): void {
			var r:int = ( this.height > this.width ) ? this.width/2 : this.height / 2;
			g.drawEllipse(this.width/2, this.height/2,  r, 0.7 * r);
		}

		public function drawProcess(): void {
			g.drawRect(0, 0, width, height);
		}
		
		public function drawDecision(): void {
    		var h2:uint = height/2;
    		var w2:uint = width/2;
      		g.moveTo(0,h2);
      		drawLine3(w2, height, width, h2, w2,0);
		}
		
		public function drawDocument(): void {
      		g.moveTo(0,height*9/10);
      		g.curveTo(width/4, height,width/2,height*9/10 );
      		g.curveTo(width*3/4,height*4/5, width, height*9/10 );
      		drawLine3(width,0,0,0,0,height/10);
		}		
		
		public function drawData(): void {
      		g.moveTo(0,height);
      		drawLine3(width/10,0, width,0, width*9/10,height);
		}
		
		public function drawPredefinedProcess(): void {
    		g.drawRect(0,0,width,height);
			g.endFill();
      		g.moveTo(width/10,0);
      		g.lineTo(width/10,height);
      		g.moveTo(width*9/10,0);
      		g.lineTo(width*9/10,height);
		}		
		
		public function drawStoredData(): void {
      		g.moveTo(width/10,0);
      		g.curveTo(0,height/2,width/10,height);
      		g.lineTo(width,height);
      		g.curveTo(width*9/10,height/2,width,0);
      		g.lineTo(width/10,0);
		}		
		
		public function drawDirectData(): void {
    		var wR:int = width*9/10;
    		var wL:int = width/10;
      		g.moveTo(wR,0);
      		g.curveTo(width, height/2, wR,height);
      		g.lineTo(wL,height);
      		g.curveTo(0, height/2, wL,0 );
      		g.lineTo(wR,0);
			g.endFill();
      		g.curveTo(width*8/10,height/2, wR,height);
		}
		
		public function drawManualInput(): void {
      		g.moveTo(0,height);
      		drawLine3(0,height/5,width,0,width,height);
		}		
		
		public function drawCard(): void {
      		g.moveTo(0,height);
      		drawLine4(0,height/2,  width/5,0, width,0, width,height);
		}		

		public function drawPaperTape(): void {
      		g.moveTo(0,height/10);
      		g.curveTo(width/4,height/5,width/2,height/10);
      		g.curveTo(width*3/4,0,width,height/10);
      		g.lineTo(width,height*9/10);
      		g.curveTo(width*3/4,height*4/5,width/2,height*9/10);
      		g.curveTo(width/4,height,0,height*9/10);
		}
		
		public function delay(): void {
      		g.moveTo(0,0);
      		g.lineTo(0,height);
      		g.lineTo(width*4/5,height);
      		g.curveTo(width,height/2, width*4/5,0);
		}
		
		public function manualOperation(): void {
      		g.moveTo(0,0);
      		drawLine3(width/5,height,width*4/5,height,width,0);
		}		

		public function preparation(): void {
      		g.moveTo(width/5,0);
      		drawLine3(0,height/2,width/5,height,width*4/5,height);
      		g.lineTo(width,height/2);
      		g.lineTo(width*4/5,0);
		}	
		
		public function terminator(): void {
    		g.drawRoundRect(x,y,width,height,width/4,height/2);
		}			
		
		public function offPage(): void {
      		g.moveTo(0,0);
      		drawLine4(0,height/2,width/2,height,width,height/2,width,0);
		}			
	}
}